package com.liaoyi.myble.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import com.liaoyi.myble.App;
import com.liaoyi.myble.R;
import com.liaoyi.myble.adapter.DeviceAdapter;
import com.liaoyi.myble.base.BaseActivity;
import com.liaoyi.myble.collector.ActivityCollector;
import com.liaoyi.myble.data.Device;
import com.liaoyi.myble.service.BluetoothService;
import com.liaoyi.myble.service.IBluetoothScan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends BaseActivity implements View.OnClickListener, IBluetoothScan {
    private static final String TAG = "MainActivity";
    private BluetoothService bluetoothService;
    private Button button;
    private RecyclerView recyclerView;
    private List<Device> devices = new ArrayList<>();
    private HashMap<String, Device> deviceHashMap = new HashMap<>();
    private DeviceAdapter deviceAdapter;
    private String[] permissions = {
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
    };

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getPermission();
        initView();

    }

    /**
     * 运行时权限
     */
    private void getPermission(){
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.
                permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, permissions, 1);
        }
    }

    private void initView() {
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(linearLayoutManager);
        deviceAdapter = new DeviceAdapter(devices);
        recyclerView.setAdapter(deviceAdapter);
    }

    private void initBluetooth() {
        bluetoothService = App.instance.bluetoothService;
        bluetoothService.setiBluetoothScan(this);
        bluetoothService.initBluetooth();
        if (bluetoothService.bluetoothAdapter.isEnabled()) {
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(intent, 1);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button:
                if(button.getText().toString().equals("开始扫描")){
                    deviceHashMap.clear();
                    devices.clear();
                    deviceAdapter.notifyDataSetChanged();
                    initBluetooth();
                    bluetoothService.startScan();
                    button.setText("停止扫描");
                }else{
                    bluetoothService.stopScan();
                    button.setText("开始扫描");
                }

                break;
        }
    }

    @Override
    public void onScanResult(BluetoothDevice bluetoothDevice) {
        if(bluetoothDevice.getName()!= null && bluetoothDevice.getName().contains("Feasy")){
            String mac = bluetoothDevice.getAddress();
            if(!deviceHashMap.containsKey(mac)){
                Device device = new Device(bluetoothDevice.getName(), mac);
                deviceHashMap.put(mac,device);
                devices.add(device);
            }
            deviceAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void stopScan() {
        button.setText("开始扫描");
    }

    @Override
    public void startScan() {
        button.setText("停止扫描");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        ActivityCollector.finishAll();
        return true;
    }
}
